package com.company.cloud.acct.modules.app.controller;

import com.company.cloud.acct.modules.app.controller.req.BankCardBindParam;
import com.company.cloud.acct.modules.app.controller.req.BankCardCodeParam;
import com.company.cloud.acct.modules.app.service.AcctBankCardService;
import com.company.cloud.acct.modules.base.service.BankCardInfoService;
import com.company.cloud.acct.modules.cache.models.BankCardModel;
import com.company.cloud.acct.modules.cache.service.AcctCacheService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
//@Login
@RestController
@RequestMapping("/app/acct/bankcard")
@Api(tags = "App_Acct_银行卡信息")
public class BankCardController {

    @Autowired
    private AcctBankCardService acctBankCardService;

    @Autowired
    private BankCardInfoService bankCardInfoService;

    @Autowired
    private AcctCacheService acctCacheService;

    @PostMapping("/getBankCardDef")
    @ApiOperation(value = "查询我的默认银行卡", response = BankCardModel.class)
    public BaseResult getBankCardDef() {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        BankCardModel data = acctBankCardService.getBankCardDefByUser(userInfo.getId());
        return BaseResult.ok(data);
    }

    @PostMapping("/getBankCardList")
    @ApiOperation(value = "查询我的银行卡", response = BankCardModel.class)
    public BaseResult getBankCardList() {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        List<BankCardModel> cardList = acctCacheService.getBankCardListByUser(userInfo.getId());
        return BaseResult.ok(cardList);
    }

    @PostMapping("/bind")
    @ApiOperation(value = "绑定银行卡", response = BaseResult.class)
    public BaseResult bindBankCard(@RequestBody BankCardBindParam param) {
        ValidatorUtils.validate(param);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        // 验证码
        acctBankCardService.validate(param.getCardMobile(), param.getVcode(), userInfo);
        BankCardModel model = acctBankCardService.addBackCard(param, userInfo);
        return BaseResult.ok(model);
    }

    @GetMapping("/updateDef")
    @ApiOperation(value = "设置默认银行卡")
    public BaseResult updateBankCardDef(@RequestParam("bankCardId") Integer bankCardId) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        acctBankCardService.updateDefBackCard(bankCardId, userInfo);
        return BaseResult.ok();
    }

    @GetMapping("/del")
    @ApiOperation(value = "解绑银行卡")
    public BaseResult delBankCard(@RequestParam("bankCardId") Integer bankCardId) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        acctBankCardService.unbindBankCard(bankCardId, userInfo);
        return BaseResult.ok();
    }

    @PostMapping("/sendBankCardCode")
    @ApiOperation(value = "发送银行卡验证码")
    public BaseResult sendBankCardCode(@RequestBody BankCardCodeParam param) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        acctBankCardService.sendBankCardCode(param, userInfo);
        return BaseResult.ok();
    }

    @GetMapping("/get")
    public BaseResult get() {
        acctBankCardService.test("250");
        return BaseResult.ok();
    }


}
